<template>
    <div>
        <cephalosome />
        <el-row class="banner">
            <h1>商品</h1>
        </el-row>
        <el-row class="main_body">
            <div class="mbodyone">
                <el-input placeholder="在这里搜索 " v-model="inputval" @change="search()" suffix-icon="el-icon-search"/>
                <h4>类别</h4>
                <p v-for="(item,index) in categorylist" :key="index" @click="getfoodlist()">
                    <router-link :to="'/shop'+(index+1)">{{item.categoryName}}</router-link>
                </p>
            </div>
            <div class="productsbox" v-if="en==0">
                <div class="productslist" v-for="(item,index) in foodlist" :key="index">
                    <div class="productslistone">
                        <img :src="'../images/'+item.foodImg" @click="single(item.foodName)">
                        <p><span>-30%</span><span class="el-icon-star-off"></span></p>
                    </div>
                    <p>{{item.foodName}}</p>
                    <p>
                      <span>1 UNIT</span> 
                      <el-rate style="display: inline-block; margin-left: 10px;"
                        v-model="value"
                        disabled
                        show-score
                        text-color="#ff9900"
                        score-template="{value}">
                      </el-rate>
                    </p>
                    <h4>￥{{item.foodPrice}}</h4>
                    <p class="car">
                      <el-input-number size="mini" style="width:115px" v-model="item.num" :min="0" :max="10" ></el-input-number>
                      <span>加入购物车</span>
                    </p>
                </div>
                <div class="pagent" v-if="pd==0">
                  <el-pagination
                    background
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    layout="total, prev, pager, next"
                    :total="total">
                </el-pagination>
                </div>
            </div>
            <router-view v-if="en==1"></router-view>
        </el-row>
        <el-row>
          <tail />
        </el-row>
    </div>
</template>
<script>
import cephalosome from './header.vue'
import tail from './demo.vue'
import {getcategorylist,selectfoodlistDesc,selectfoodlistByName} from '../api/api.js' 

export default {
    name:"shop",
    data(){
        return{
            categorylist:[],
            foodlist:[],
            foodcategorylist:{},
            value:1.5,
            inputval:"",
            pageNum: 1, // 当前页码
            pageSize: 10,   // 每页显示的数量
            total: 0,
            en:0,
            pd:0
        }
    },
    components:{
        cephalosome,
        tail
    },
    methods:{
        getcategory(){
            let that=this;
            getcategorylist().then(res => {
                that.categorylist = res;
                console.log(that.categorylist);
            }).catch(error => {
              console.log("错误信息:"+error);
            })
        },
        //处理currentPage的变化
        handleCurrentChange(pageNum){
          // alert(pageNum)
          this.pageNum = pageNum
          this.getfoodlistbydesc()
        },
        getfoodlistbydesc(){
            let that = this;
            selectfoodlistDesc(this.pageNum,this.pageSize).then(res => {
              if (res.code!==null) {
                console.log("------");
                console.log(res);
                that.total = res.data.total;
                that.foodlist = res.data.records;
                that.foodlist = res.data.records.map(item => ({ ...item, num: 0 }));
                that.en=0;
              } else {
                console.log("请求失败:", res.msg);
              }
            }).catch(error => {
              console.log("错误信息:"+error);
            })
        },
        search(){
          const that=this;
           if(that.inputval!=null){
                alert(that.inputval)
                console.log(that.inputval);
                
                selectfoodlistByName(that.inputval).then(res => {
                  if (res.status === 200) {
                    that.en=0;
                    that.pd=1;
                    that.foodlist = [];
                    that.foodlist.push(res);
                    console.log(that.foodlist);
                    
                    that.foodlist = that.foodlist.map(item => ({ ...item, num: 0 }));
                  } else {
                    console.log("请求失败:", res.msg);
                  }
                }).catch(error => {
                  console.log("错误信息:"+error);
                })
            }
        },
        getfoodlist(){
          this.en=1;
          console.log(this.en);
          
        },
        single(name){
           this.$router.push({ name: 'single', params: { name: name } });
           
        }

    },created() {
      this.getcategory();
      this.getfoodlistbydesc();
      console.log("你好这是购物车");
}
}
</script>
<style scoped>
.banner{
    width: 100%;
    height: 300px;
    background-image: url("../images/banner-1.jpg");
    background-repeat: no-repeat;
    margin: 0 auto;
    background-size: cover;
    overflow: hidden;
    background-color: #f4d16e;
}
.banner h1{
  line-height: 300px;
  padding: 0 50px;
}
.banner div{
    width: 1600px;
    height: 300px;
    margin: 0 auto;
}
.main_body{
    width: 1600px;
    height: 850px;
    margin: 0 auto;
}
.mbodyone{
    float: left;
    width: 210px;
    height: 740px;
    display: inline-block;
    margin: 20px;
}
.mbodyone h4{
    font-size: 24px;
    margin: 10px 0;
}
.mbodyone p{
    font-size: 16px;
    color: gray;
}
.mbodyone .el-input--small .el-input__inner {
    font-size: 17px;
    height: 47px;
    line-height: 32px;
}
.el-icon-search{
    margin-right: 5px;
    font-size: 23px;
}
.productsbox{
    float: left;
    width: 1250px;
    height: 800px;
    display: inline-block;
    margin: 20px 30px;
}
.productslist{
    width: 200px;
    height: 320px;
    float: left;
    padding: 15px;
    margin: 10px 15px 10px 0;
    border-radius: 15px;
}
.productslist:hover{
    box-shadow: 0px 21px 44px rgba(0, 0, 0, 0.08);
  }
  .productslist p{
    line-height: 20px;
    margin: 10px 0 0 0;
  }
  .productslistone{
      border-radius: 10px;
      width: 200px;
      height: 170px;
      margin: 0;
  }
  .productslistone p{
    float: left;
    position: relative;
    bottom: 170px;
    margin: 0;
    height: 0;
    line-height: 0px;
    margin: 0 10px;
  }
  .productslistone img{
    margin: 0 auto;
    width: 200px;
    height: 170px;
    border-radius: 15px;
  }
  .productslistone span:first-child{
    float: left;
    width: 50px;
    display: inline-block;
    height: 25px;
    line-height: 25px;
    color: white;
    background-color: #a3be4c;
    font-weight: bold;
    text-align: center;
    margin: 10px 5px;
    font-size: 12px;
    border-radius: 8px;
  }
  .productslistone span:last-child{
    width: 45px;
    height: 45px;
    display: inline-block;
    border-radius: 45px;
    line-height: 45px;
    border: #d2d4d4 1px solid;
    margin: 0 10px 0 60px;
  }
  .productslistone span:last-child:hover{
    background-color: #f03838;
    color: white;
  }
  .el-icon-star-off{
    font-size: 23px;
    font-weight: bold;
    text-align: center;
  }
  .productslist h4{
    font-size: 25px;
    margin: 0;
  }
  .productslist p:nth-child(3){
    color: #5f696a;
    font-size: 13px;
  }
  .car span{
    float: right;
    color: #5f696a;
    font-size: 12px;
    margin: 0 10px;
  }
  .pagent{
    float: left;
    width: 1250px;
    height: 50px;
    margin: 0 auto;
  }
  .el-pagination{
    margin: 0 auto; 
    width: 200px;
  }
  a{
    text-decoration: none;
    color: gray;
}
</style>
